<template>
  <div class="w h over-h">
    <div class="category-title a-i-c f-s dialog-page-title">
      <el-button type='text' icon="ArrowLeft" @click="goBackToPage">返回</el-button>
      <div class="m-l10">
        {{ params.isEdit ? '菜单编辑' : '菜单新增' }}
      </div>
    </div>
    <div class="w f-c a-i-c" :style="normalBodyHeight">
      <div style="width:600px">
        <lm-form :ref="editFormRefKey" inline label-width="120px" class="lm-w lm-h"
          :layout="editFromLayout" v-model="editForm">
          <template #meta>
            <span class="c7">
              Meta:{{ metaString }}
            </span>
          </template>
        </lm-form>
        <div class="lm-w lm-h lm-f-s">
          <div class="lm-w-120 lm-h"></div>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button type="danger" @click="reset" plain>重置</el-button>
          <el-button type="primary" @click="goBackToPage()" plain>返回</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import frameProperty from '@/mixins/frame-property'
import { Menu, type MenuInfo } from '@/apis/menu/index'
import { RequestCode } from '@/server/base'
import { ValidateFieldsError } from 'async-validator'

export default {
  name: 'MenuEdit',
  mixins: [frameProperty],
  props: {
    goBackToPage: Function,
    params: [Object, Array]
  } as any,
  data() {
    return {
      editFormRefKey: 'menuModifyRef',

      editForm: {} as {} | MenuInfo | any,
      editFromLayout: [
        {
          label: '所属目录',
          prop: 'parentId',
          type: 'select',
          itemClass: 'column_2',
          message: '请输入2-10个字符',
          required: true,
          clearable: true
        },
        {
          prop: 'elIcon',
          label: '图标',
          type: 'input',
          itemClass: 'column_2',
          message: '请输入2-10个字符',
          clearable: true,
          regex: /^.{0,20}$/
        },
        {
          prop: 'name',
          label: '菜单Code',
          type: 'input',
          itemClass: 'column_2',
          message: '请输入2-20个字符',
          required: true,
          clearable: true,
          regex: /^.{0,20}$/
        },
        {
          prop: 'label',
          label: '菜单名称',
          type: 'input',
          itemClass: 'column_2',
          message: '请输入2-20个字符',
          required: true,
          clearable: true,
          regex: /^.{0,20}$/
        },

        {
          prop: 'hidden',
          label: '是否隐藏',
          type: 'radio-button',
          itemClass: 'column_2',
          required: true,
          clearable: true,
          options: [
            { label: '是', value: '1' },
            { label: '否', value: '0' }
          ],
          value: '0'
        },
        {
          prop: 'type',
          label: '菜单类型',
          type: 'select',
          itemClass: 'column_2',
          message: '请输入2-100个字符',
          required: true,
          clearable: true,
          options: [
            { label: '菜单', value: '1' },
            { label: '按钮', value: '2' }
          ],
          value: '1'
        },
        {
          prop: 'item',
          label: '路由地址',
          type: 'input',
          itemClass: 'column_2',
          message: '请输入2-100个字符',
          clearable: true,
          regex: /^.{0,100}$/
        },
        {
          prop: 'sort',
          label: '排序',
          type: 'input',
          itemClass: 'column_2',
          message: '请输入0-9999的数字',
          clearable: true,
          regex: /^.{0,4}$/
        },
        {
          label: '',
          itemClass: 'column_1 tip-p b-p2 h-36 f-s a-i-c',
          type: 'div',
          innerHTML: '弹出层属性'
        },
        {
          prop: 'width',
          label: '弹出层宽度',
          type: 'input',
          itemClass: 'column_2',
          message: '请输入2-6个字符',
          clearable: true,
          value: ''
        },
        {
          prop: 'customLabel',
          label: '是否自定义标题',
          title: '只对弹出页面有效',
          type: 'radio-button',
          itemClass: 'column_2',
          message: '请输入2-20个字符',
          clearable: true,
          options: [
            { label: '是', value: '1' },
            { label: '否', value: '0' }
          ],
          value: '0'
        },
        {
          prop: 'handler',
          label: '阻止打开页面',
          title: '只对弹出页面有效',
          type: 'radio-button',
          itemClass: 'column_2',
          message: '请输入2-20个字符',
          clearable: true,
          options: [
            { label: '是', value: '-1' },
            { label: '否', value: '1' }
          ],
          value: '1'
        },
        {
          prop: 'meta',
          label: '',
          title: '只对弹出页面有效',
          placeholder: 'meta',
          type: 'slot',
          disabled: true,
          itemClass: 'column_2'
        }

        // {
        //   prop: 'meta',
        //   label: '其他参数',
        //   type: 'textarea',
        //   rows: 10,
        //   itemClass: 'column_1',
        //   message: '请输入2-100个字符',
        //   placeholder: '请输入JSON格式的字符串',
        //   clearable: true,
        //   regex: /^.{0,100}$/
        // }
      ] as any[]
    }
  },
  computed: {
    modifyInfo() {
      let { meta, ...row } = this.params.row || {}
      if (meta && typeof meta == 'string') {
        meta = eval(`(${meta})`)
      }
      row.meta = meta || {}
      return row
    },
    metaString() {
      const { label, customLabel, hidden, width, handler, elIcon } = this.editForm
      let meta = { label } as any
      if (+hidden == 1) {
        meta.hidden = true
      }
      if (+customLabel) {
        meta.customLabel = true
      }
      if (!!width) {
        meta.width = width
      }
      if (!!elIcon) {
        meta.elIcon = elIcon
      }
      if (handler == -1) {
        meta.handler = -1
      }
      return JSON.stringify(meta)
    }
  },
  methods: {
    getDataList() {
      Menu.list()
        .then((res: RequestCode) => {
          this.editFromLayout[0].options = [{ label: '顶级目录', value: 0 }].concat(
            res.data.map((item: any) => {
              let metaStr = item.meta
              if (metaStr && typeof metaStr == 'string') {
                const { label } = eval(`(${metaStr})`)
                return {
                  label,
                  value: item.id
                }
              }
              return {
                label: '-',
                value: item.id
              }
            })
          )
        })
        .catch(() => {})
    },
    getDetailById() {
      if (!this.modifyInfo) {
        return
      }
      let { type, meta, parentId, ...params } = this.modifyInfo
      const { label, customLabel, hidden, width, handler, elIcon } = meta
      this.editForm = {
        ...params,
        elIcon,
        type: type == '2' ? '2' : '1',
        parentId: +parentId || 0,
        label,
        customLabel: +(customLabel == 'true'),
        hidden: hidden || hidden == 'true' || hidden == '1' ? '1' : '0',
        width: width || '',
        handler: handler == '-1' || handler == 'true' ? '-1' : '1'
      }
    },
    saveMenuInfo() {
      const { label, customLabel, hidden, width, handler, elIcon, children, ...params } =
        this.editForm
      params.meta = this.metaString
      const method = this.params.isEdit ? Menu.modifyMenuInfo : Menu.addMenuInfo
      method(params)
        .then((res: RequestCode) => {
          this.showMessage({ type: 'success', message: '操作成功！' })
          this.goBackToPage(true)
        })
        .catch((error: any) => {})
    },
    //验证数据
    submit() {
      const editFormRef = this.$refs[this.editFormRefKey] as any
      editFormRef.validate((isValid: boolean, invalidFields?: ValidateFieldsError) => {
        if (!isValid) {
          //滚动到未通过校验的字段
          if (invalidFields && Object.keys(invalidFields).length > 0) {
            editFormRef.scrollToField(Object.keys(invalidFields)[0])
          }
          return
        }
        this.saveMenuInfo()
      })
    },
    //重置
    reset() {
      this.editForm = {}
    }
  },
  created() {
    this.$nextTick(() => {
      this.getDetailById()
      this.getDataList()
    })
  }
}
</script>

<style scoped></style>